<template>
  <div class="goods">
    <van-list
      v-model="loading"
      :finished="finished"
      :offset="80"
      @load="loadMore">
      <div v-for="(item, key) in goodsList" :key="key" class="goods-list">
        <div class="left_con" @click="clickToGoodsDetail(item.item_id)">
          <img v-if="item.image_url" :src="item.image_url" alt="">
          <img v-else src="../../../../assets/item-default.png" alt="">
        </div>
        <div class="right_con">
          <div class="title" @click="clickToGoodsDetail(item.item_id)">{{ item.title }}</div>
          <!--<span style="color: #a1a1a1;">起订量:{{ item.mininum?item.mininum:'-' }} ,限购量:{{ item.maxnum?item.maxnum:'-' }}</span>-->
          <div class="price font-colorRed">
            <span class="unit">&yen;</span>
            <span class="txt">{{ Number(item.price).toFixed(getitem.item_price_p) }}</span>
          </div>
          <div class="market-price">
            <span class="txt">建议零售价: &yen;{{ Number(item.market_price).toFixed(getitem.item_price_p) }}</span>
            <div v-if="item.sku_lists.length === 0" class="fr addBtn" @click="openBottomList(item)">
              <img class="addBtnImg" src="@/assets/images/goodsImg/addBtn.png" alt="">
            </div>
            <div v-else class="fr checkedSku" @click="openBottomList(item)">
              <span class="btn">选规格</span>
            </div>
          </div>
        </div>
      </div>
    </van-list>
    <div v-if="goodsList.length == 0 && !loading" class="noData small">
      <div class="content">
        <img src="../../../../assets/images/noData/noDataGoods.png" alt="">
        <div class="text">暂无商品</div>
      </div>
    </div>
    <goods-bottom-dialog ref="goodsDialog"/>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import goodsBottomDialog from '../goodsBottomDialog'
import { getItemList } from '@/api/goods'
export default {
  name: 'Index',
  components: {
    goodsBottomDialog
  },
  data() {
    return {
      getAllData: false, // 为true时加载完全部数据
      goodsList: [],
      loading: false,
      finished: false,
      isGetListComplete: false
    }
  },
  computed: {
    ...mapGetters([
      'getitem'
    ])
  },
  methods: {
    loadMore() {
      this.loading = true
      this.getItemListFn()
    },
    getItemListFn() {
      getItemList(this.$parent.getItemListPostData).then(res => {
        this.loading = false
        const data = res.data
        if (data.status_code === 200) {
          this.$parent.getItemListPostData.p++
          this.goodsList = [...this.goodsList, ...data.data.list] // 数组拼接
          // 单次请求数据完毕
          if (this.goodsList.length >= data.data.count) {
            this.finished = true
            this.getAllData = true
          }
          this.isGetListComplete = true
        } else {
          this.goodsList = []
          this.finished = true
          this.isGetListComplete = true
        }
      })
    },
    // 点击进入商品详情
    clickToGoodsDetail(id) {
      this.$router.push({ name: 'goods-detail', query: { goodsId: id }})
    },
    // 点击选择sku
    openBottomList(goodsItem) {
      this.$refs.goodsDialog.isOpen = true
      this.$refs.goodsDialog.goodsItem = goodsItem
      this.$refs.goodsDialog.type = 1
      // console.log('goodsItem=', goodsItem)
      if (goodsItem.sku_lists && goodsItem.sku_lists.length > 0) {
        goodsItem.sku_lists.forEach(sku => {
          this.$set(sku, 'buyNum', 0)
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
  @import "src/styles/mixin.scss";
  .goods{
    padding-left: pxCalc(20px);
    background: #fff;
    &-list{
      border-bottom: pxCalc(1px) solid #f5f5f5;
      padding: pxCalc(15px) 0;
      position: relative;
      .left_con{
        display: inline-block;
        width: pxCalc(100px);
        height: pxCalc(100px);
        vertical-align: top;
        border: pxCalc(1px) solid #f5f5f5;
        border-radius: pxCalc(5px);
        overflow: hidden;
        img{
          display: block;
          width: 100%;
          height: 100%;
        }
      }
      .right_con{
        display: inline-block;
        vertical-align: top;
        width: pxCalc(220px);
        margin-left: pxCalc(15px);
        .title{
          font-size: pxCalc(15px);
          color: #363b43;
          padding-right: 1em;
          word-break:break-all;
          display: -webkit-box;
          overflow: hidden;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .price{
          margin-top: pxCalc(10px);
          font-size: pxCalc(12px);
          .txt{
            font-weight: bold;
            font-size: pxCalc(17px);
          }
        }
        .market-price{
          .txt{
            color: #999999;
          }
          .addBtn{
            margin-top: pxCalc(-6px);
            width: pxCalc(22px);
            position: absolute;
            bottom: pxCalc(15px);
            right: pxCalc(20px);
            img{
              width: 100%;
            }
          }
          .checkedSku{
            margin-top: pxCalc(-6px);
            position: absolute;
            bottom: pxCalc(15px);
            right: pxCalc(20px);
            background-color: #ff7340;
            padding: pxCalc(2px) pxCalc(5px);
            border-radius: pxCalc(50px);
            .btn{
              color: #fff;
              font-size: 12px;
              display: inline;
            }
          }
        }
      }
    }
  }
</style>
